// pages/register/register.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    index: null,
    picker: ['男', '女'],
    username: '',
    password: '',
    nickname: '',
    passwordAgain: '',
    sex: ''
  },

  pickerChange(e){
    this.setData({index: e.detail.value});
  },

  myUsername: function(e){
    this.setData({username: e.detail.value})
  },

  myNickname: function(e){
    this.setData({nickname: e.detail.value})
  },
  myPassword: function(e){
    this.setData({password: e.detail.value})
  },
  myPasswordAgain: function(e){
    this.setData({passwordAgain: e.detail.value})
  },

  //校验用户名、密码的合法性
  isLegal: function(username, password){
    // var phone = this.data.username;
    console.log(this.data.username);
    if(!(/^1\(3|4|5|6|7|8|9\)d{9}$/).test(username)){ 
        wx.showToast({
          title: '用户名不合法，请重新输入',
          icon: 'none',
          duration: 2000
        });
        return false; 
    } else if(!(/^[a-zA-Z]\w{5,17}$/).test(password)){
      wx.showToast({
        title: '以字母开头，长度在6-18之间，只能包含字符、数字和下划线',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    return true;
  },

  //非空判断
  isNone: function(){
    if(this.data.username.length == 0){
      wx.showToast({
        title: '用户名不能为空',
        icon: 'none',
        duration: 2000
      });
      return true;
    } else if (this.data.nickname.length == 0){
      wx.showToast({
        title: '昵称不能为空',
        icon: 'none',
        duration: 2000
      });
      return true;
    } else if (this.data.password.length == 0){
      wx.showToast({
        title: '密码不能为空',
        icon: 'none',
        duration: 2000
      });
      return true;
    } else if (!this.data.index){
      wx.showToast({
        title: '性别不能为空',
        icon: 'none',
        duration: 2000
      });
      return true;
    } else if(this.data.password != this.data.passwordAgain){
      wx.showToast({
        title: '输入的密码不一致，请重新输入！',
        icon: 'none',
        duration: 2000
      })
      return true;
    }
    return false;
  },

  isSame: function(){
    if(this.data.password != this.data.passwordAgain){
      wx.showToast({
        title: '输入的密码不一致，请重新输入！',
        icon: 'none', 
        duration: 2000
      })
      return;
    }
  },

  register: function(){
    var that = this;
    if(!this.isNone()){
      if(this.isLegal(that.data.username, that.data.password)){
        wx.request({
          url: 'http://localhost:8080/user/register',
          method: 'GET',
          data:{
            'username': that.data.username,
            'nickname': that.data.nickname,
            'password': that.data.password,
            'sex': that.data.picker[that.data.index]
          },
          success: function(res){
            if(res.data){
              wx.showToast({
                title: '注册成功！',
                icon: 'success',
                duration: 2000,
                success: function(){
                  wx.navigateTo({
                    url: '../login/login',
                  })
                }
              })
            } else {
              wx.showToast({
                title: '该手机号已注册，请更换用户名！',
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
      
    }
    //console.log("Yes!");
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})